package cn.mcch.investigator.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import cn.mcch.investigator.application.InvestigatorApplication;
import cn.mcch.investigator.dialog.ExitDialog;
import cn.mcch.investigator.R;
import cn.mcch.investigator.fragment.FragmentFactory;
import cn.mcch.investigator.model.impl.MainModelImpl;
import cn.mcch.investigator.observer.InvestigatorObserverable;
import cn.mcch.investigator.presenter.impl.MainPresenterImpl;
import cn.mcch.investigator.service.SmsService;
import cn.mcch.investigator.utils.DialogUtils;
import cn.mcch.investigator.utils.ToastUtils;
import cn.mcch.investigator.viewdelegate.impl.MainViewDelegateImpl;

/**
 * Created by Administrator on 2016/12/16.
 */
public class MainActivity extends BaseActivity<MainViewDelegateImpl, MainModelImpl, MainPresenterImpl> implements OnPageChangeListener {

    private Handler mHandler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            isExit = false;
        }
    };
    private boolean isExit;
    private ExitDialog dialog;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        startService(new Intent(getApplicationContext(), SmsService.class));
    }

    @Override
    public Class<MainPresenterImpl> getPresenterClass() {
        return MainPresenterImpl.class;
    }

    @Override
    public void createFirstView() {
        mPresenter.createFirstView(getApplicationContext(), R.layout.activity_main, this);
    }

    @Override
    public void initTitleBar() {
        mPresenter.initTitleBar(getApplicationContext(), isTranslucentStatus, "创建任务", R.drawable.menu, 0, R.color.login_btn_text, this);
    }

    @Override
    public void initBindData() {
        mPresenter.setUsername(InvestigatorApplication.getInstance().getUserBeanbean().user_name);
        mPresenter.setViewPagerAdapter(getSupportFragmentManager());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_titlebar_left:
                // 打开或关闭drawerlayout
                mPresenter.toogle();
                break;
            case R.id.tv_create_task:
                // 创建任务
                mPresenter.setTitleBarText("创建任务");
                mPresenter.toogle();
                mPresenter.setCurrentItem(0);
                break;
            case R.id.tv_my_task:
                // 我的任务
                mPresenter.setTitleBarText("我的任务");
                mPresenter.toogle();
                mPresenter.setCurrentItem(1);
                if(!FragmentFactory.getFragmentFromPosition(1).isLoadData()) {
                    FragmentFactory.getFragmentFromPosition(1).initData();
                }
                break;
            case R.id.tv_find:
                // 综合查询
                mPresenter.setTitleBarText("综合查询");
                mPresenter.toogle();
                mPresenter.setCurrentItem(2);
                break;
            case R.id.tv_update_password:
                // 修改密码
                mPresenter.setTitleBarText("修改密码");
                mPresenter.toogle();
                mPresenter.setCurrentItem(3);
                break;
            case R.id.tv_exit:
                // 弹出是否退出当前用户对话框
                mPresenter.toogle();
                dialog = DialogUtils.showExitDialog(getSupportFragmentManager(), "确定要退出当前用户吗?", "取消", "退出", this);
                break;
            case R.id.sb_exit:
                // 退出当前用户，回到登录界面
                mPresenter.exit(dialog, this);
                this.finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageSelected(int position) {
        String title = null;
        switch (position) {
            case 0:
                title = "创建任务";
                break;
            case 1:
                title = "我的任务";
                if(!FragmentFactory.getFragmentFromPosition(1).isLoadData()){
                    FragmentFactory.getFragmentFromPosition(1).initData();
                }
                break;
            case 2:
                title = "综合查询";
                break;
            case 3:
                title = "修改密码";
                break;
        }
        mPresenter.setTitleBarText(title);
    }

    @Override
    public void onBackPressed() {

        if (mPresenter.isDrawerpen()) {
            mPresenter.toogle();
            return;
        }
        if (!isExit) {
            isExit = true;
            mHandler.postDelayed(runnable, 2000);
            ToastUtils.showToast("再按一次返回键退出应用");
        } else {
            // 退出前清空数据
            FragmentFactory.clear();
            InvestigatorObserverable.clearAllObservers();
            super.onBackPressed();
        }
    }
}
